<?php
/**
 * @file fashionwear.php
 * @author LinLin
 * @date 2016年11月22日 上午10:38:19
 * @brief 
 */
namespace haibao\cms\data\fashionwear;

use haibao\cms\model\filter\activity\Activity as ActivityFilter;

class FashionWear extends \haibao\cms\data\BaseMysql{

    public function __construct(){
        parent::__construct('\haibao\cms\model\data\fashionwear\FashionWear');
    }

    public function getNewFashionWear(){
        $fashionWearId = '';
        $sql = 'SELECT
                	a.Id,
                	count(a.Id) as cnt
                FROM
                	FashionWear AS a
                LEFT JOIN FashionWearProduct AS b ON a.Id = b.FashionWearId
                WHERE
                	a.`Status` = 1
                GROUP BY
                	a.Id
                ORDER BY
                	a.UpdateTime DESC';
        $result = $this->query($sql);
        $fashionWearProduct = array();
        while($row = $result->fetch_assoc()){
            $fashionWearProduct[$row['Id']] = $row['cnt'];
        }
        if($fashionWearProduct){
           foreach($fashionWearProduct as $key=>$val){
                if($val >= 3){
                    $fashionWearId = $key;
                    break;
                }
            }
        }
        return $fashionWearId;
    }
    
    public function getCountFashionWear(){
        $sql = 'select count(Id) as cnt from FashionWear where Status = 1 and Published = 1';
        $resultCount = $this->query($sql)->fetch_assoc();
        return $resultCount['cnt'];
    }
    
    public function getNearCurrentId($currentId,$starId){
        $temp = '';
        if($starId){
            $temp = ' and StarId = '.$starId;
        }
        $fashionWearNext = array();
        $sqlNext = 'select Id,StarId,UserDefinedStarName from FashionWear where UpdateTime > (select UpdateTime from FashionWear where Id = %s) and Status = 1 and Published =1 '.$temp.' order by UpdateTime desc limit 11';
        $resultNext = $this->query($sqlNext,array($currentId));
        while($rowNext = $resultNext->fetch_assoc()){
            $fashionWearNext[$rowNext['Id']] = array('StarId'=>$rowNext['StarId'],'StarName'=>$rowNext['UserDefinedStarName']);
        }
        $sqlPre = 'select * from FashionWear where UpdateTime <= (select UpdateTime from FashionWear where Id = %s)  and Status = 1 and Published =1 '.$temp.' order by UpdateTime desc limit 11';
        $resultPre = $this->query($sqlPre,array($currentId));
        while($rowPre = $resultPre->fetch_assoc()){
            $fashionWearNext[$rowPre['Id']] = array('StarId'=>$rowPre['StarId'],'StarName'=>$rowPre['UserDefinedStarName']);
        }
        return $fashionWearNext;
    }
    
    public function getOneFashionWearByStarId($starId){
        $sql = 'select Id,ImageId,PublishTime from FashionWear where StarId = %s and Status = 1 and Published = 1 order by UpdateTime desc limit 1';
        $fashionWearInfo = array();
        $fashionWearInfo = $this->query($sql,array($starId))->fetch_assoc();
        return $fashionWearInfo;
    }
    

    /**
     * 通过图片id数组获得时尚穿搭 关联明星及关联单品 的信息
     * @param unknown $imageIds 图片数组
     */
    public function getFashionProductInfoByImgIds($imageIds){
        $imageIdArr = explode(',',$imageIds);
        $sql = 'select a.Id,a.ImageId,b.ProductId,b.ProductName,b.IsShowProductName from FashionWear as a left join FashionWearProduct as b on a.Id = b.FashionWearId where a.ImageId in ('.$imageIds.')';
        $result = $this->query($sql,$imageIdArr);
        $fashionWearInfo = $productId = array();
        while($row = $result->fetch_assoc()){
            $fashionWearInfo[$row['ImageId']]['rightimage'][] = $row;
            $productId[] = $row['ProductId'];
        }
        return array($fashionWearInfo,$productId);
    }

    /**
     * 通过图片ID数组 获得分享图片（sharepicurl）地址
     * @param unknown $imageIds
     */
    public function getSharePicUrlByImgIds($imageIds){
        $imageIdArr = explode(',',$imageIds);
        $sql = 'select Id,ImageId,SharePicUrl from FashionWear where ImageId in ('.$imageIds.')';
        $result = $this->query($sql,$imageIdArr);
        $fashionWearInfo = array();
        while($row = $result->fetch_assoc()){
            $fashionWearInfo[] = $row;
        }
        return $fashionWearInfo;
    }

    /**
     * 通过时尚穿搭ID获取时尚穿搭信息
     * @param unknown $fashionwearid
     * @return NULL
     */
    public function getOneFashionWearById($fashionwearid){
        return $this->getOneById($fashionwearid);
    }

    /**
     * 时尚搭配合并图片
     * @param unknown $fashionwearid 时尚穿搭id
     */
    public function combineFashionWearPic($fashionwearid){
        $sql = 'select FashionWearId,ProductId,ProductOrder from FashionWearProduct where FashionWearId = %s';
        $result = $this->query($sql,array($fashionwearid));
        $productIds = array();
        while($row = $result->fetch_assoc()){
            $productIds[$row['ProductId']] = $row['ProductOrder'];
        }
        return $productIds;
    }

    /**
     * All 通过实体获得时尚穿搭主表信息
     * @param unknown $query
     * @return multitype:
     */
    public function getFashionWearByFilter($query){
        return $this->getAll($query);
    }

    /**
     * 更改时尚穿搭图片状态（删除）
     * 在时尚穿搭表中 更改图片状态   在imagelibrary表中更改 IsFashionWear状态
     * @param unknown $id 图片id
     * @param unknown $status 状态
     * @throws BusinessException
     */
	public function updateImageStatus($id, $imageId, $status = \haibao\cms\model\data\ImageLibrary::STATUS_ENABLE, $userName = ''){
		$sql = 'update `FashionWear` set Status = %s where Id = %s';
		$this->query($sql, array($status,$id));

		$sql = 'update `ImageLibrary` set IsFashionWear = 0 where Id = %s';
		$this->query($sql,array($imageId));

		$sql = 'delete from `FashionWearProduct` where FashionWearId = %s';
		$this->query($sql,array($id));
	}


	/**
	 * 保存时尚穿搭主表信息
	 * @param unknown $model
	 */
    public function saveFashionWear($model){
        \haibao\cms\business\ImageLibrary::updateImageIsFashionWear($model->ImageId);
        if($model->Id){
            $this->updateById($model->Id,$model);
        }else{
            $this->add($model);
            $this->addProperty($model->Id);
        }
        return $model->Id;
    }
    
    private function addProperty($fashionWearId){
        $this->query('insert into `FashionWearProperty` (FashionWearId) values (%s)' , array($fashionWearId));
    }
    
    /**
     * 通过图片ID获得时尚穿搭主表信息
     * @param unknown $imageId
     */
   public function getOneFashionWearByImgId($imageId,$isPublished = false){
       if($isPublished == true){
           $sql = 'select Id,Title,ImageId,StarId,PublishTime,UserDefinedStarName from FashionWear where ImageId = %s and Status = 1 and Published = 1 limit 1';
       }else{
           $sql = 'select Id,Title,ImageId,StarId,PublishTime,UserDefinedStarName from FashionWear where ImageId = %s and Status = 1 limit 1';
       }
       return $this->query($sql,array($imageId))->fetch_assoc();
   }
   
   /**
    * 通过图片ID获得已经发布的时尚穿搭信息
    * @param unknown $imageId
    */
   public function getOneHasPublishFashionWear($fashionWearId){
       $fashionWearProduct = array();
       $sql = 'select Id,StarId,SharePicUrl from FashionWear where ImageId = %s and Status = 1 limit 1';
       $fashionWearId = $this->query($sql,array($fashionWearId))->fetch_assoc();
       return $fashionWearId;
   }

   public function updateSharePicUrlByFashionWearId($fashionwearid,$sharePicUrl){
       $sql = 'update `FashionWear` set SharePicUrl = %s where Id = %s';
       $this->query($sql,array($sharePicUrl,$fashionwearid));
   }
}